<?php

namespace addons\TfFutures\backend\controllers;

use addons\TfFutures\common\models\Bankinfo;
use addons\TfFutures\common\models\SymbolCate;
use yii;
use common\components\Curd;
use yii\data\Pagination;

class CardController extends BaseController
{
    use Curd;
    public $modelClass = Bankinfo::class;

    public function actionIndex()
    {
        $data = Bankinfo::find();
//        $bank_code = Yii::$app->request->get('bank_code');
//
//        if (!empty($bank_code)) {
//            $data->andWhere(['bank_code' => $bank_code]);
//        }
//        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
//
//        $models = $data->offset($pages->offset)
//            ->orderBy('id desc')
//            ->limit($pages->limit)
//            ->all();
        $model = $data->where(['type'=>2])->one();
        return $this->render($this->action->id, [
//            'models' => $models,
            'model' => $model,
        ]);
    }


    /**
     * 返回模型
     *
     * @param $id
     * @return \yii\db\ActiveRecord
     */
    protected function findModel($id)
    {
        /* @var $model \yii\db\ActiveRecord */
        if (empty($id) || empty(($model = $this->modelClass::find()->where(['id' => $id])->one()))) {
            $model = new $this->modelClass;
            return $model->loadDefaultValues();
        }

        return $model;
    }

    /**
     * 删除
     *
     * @param $id
     * @return mixed
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelete($id)
    {
        if ($this->modelClass::findOne($id)->delete()) {
            return $this->message("删除成功", $this->redirect(['index']));
        }

        return $this->message("删除失败", $this->redirect(['index']), 'error');
    }

    /**
     * 添加商户银行卡信息
     * @return mixed|string
     * @throws yii\db\Exception
     */
    public function actionInsertEdit()
    {
        $model = new Bankinfo();
        //ajax验证
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
            $model->type = '2';
            $model->status = '2';
            $model->created_at = time();
            $model->updated_at = time();
            $model->member_id = 0;
            if($model->save()){
                return $this->message("添加成功", $this->redirect(['index']), 'success');
            }else{
                return $this->message("失败", $this->redirect(['index']), 'error');
            }
        }
        return $this->renderAjax('insert-edit', ['model' => $model]);
    }

    /**
     * 修改商户银行卡信息
     * @return mixed|string
     * @throws yii\db\Exception
     */
    public function actionUpdateEdit()
    {
        //找出商户银行卡的信息
        $model = Bankinfo::find()->where(['type'=>2])->one();
        $data = Yii::$app->request->post();
        // ajax 校验
        $this->activeFormValidate($model);
        if($model->load(Yii::$app->request->post())){
            $model->updated_at = time();
            if($model->save()){
                return $this->message("修改成功", $this->redirect(['index']), 'success');
            }else{
                return $this->message("失败", $this->redirect(['index']), 'error');
            }
        }
        return $this->renderAjax('update-edit',['model'=>$model]);
    }
}